package com.cc.dao.impl;

import com.cc.dao.BaseDao;
import com.cc.dao.EmpDao;
import com.cc.entity.Emp;
import com.cc.util.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.SynchronousQueue;

/**
 * @author 33314
 * @version 1.0
 * @description: TODO
 * @date 2025/4/18 09:00
 */
public class EmpDaoImpl extends BaseDao implements EmpDao {
    @Override
    public Page<Emp> getEmp(Page<Emp> empPage, String empName) {
        List<Emp> empList = new ArrayList<>();
        List<Object> objectList = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT");
        sb.append(" e.*, ");
        sb.append(" d.dep_name  deptName  ");
        sb.append(" FROM ");
        sb.append(" employee e, ");
        sb.append(" deptment d  ");
        sb.append(" WHERE ");
        sb.append(" e.dep_id = d.id ");
        if (empName != null && !"".equals(empName)) {
            sb.append(" AND ");
            sb.append(" e.emp_name LIKE CONCAT ('%',?,'%') ");
        }
        sb.append(" ORDER BY e.id ");
        sb.append(" LIMIT ?,? ");
        objectList.add((empPage.getPageIndex() - 1) * empPage.getPageSize());
        objectList.add(empPage.getPageSize());
        ResultSet rs = executeQuery(String.valueOf(sb), objectList.toArray());
        if (rs != null) {
            try {
                while (rs.next()) {
                    Emp emp = new Emp();
                    emp.setId(rs.getInt("id"));
                    emp.setEmpName(rs.getString("emp_name"));
                    emp.setDeptId(rs.getInt("dep_id"));
                    emp.setDeptName(rs.getString("deptName"));
                    emp.setPosition(rs.getString("position"));
                    emp.setStartDate(rs.getString("start_date"));
                    emp.setPhone(rs.getString("phone"));
                    emp.setImgUrl(rs.getString("img_url"));
                    empList.add(emp);
                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                closedAll(rs);
            }
        }
        empPage.setData(empList);
        empPage.setTotalCount(getTotalCount(empName));
        return empPage;
    }

    @Override
    public int getTotalCount(String empName) {
        int count = 0;
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ");
        sb.append(" COUNT(1) count ");
        sb.append(" FROM ");
        sb.append(" employee e, ");
        sb.append(" deptment d  ");
        sb.append(" WHERE ");
        sb.append(" e.dep_id = d.id ");
        if (empName != null && !"".equals(empName)) {
            sb.append(" AND ");
            sb.append(" e.emp_name LIKE '%"+empName+"%' ");
        }
        ResultSet rs = executeQuery(String.valueOf(sb));
        if (rs != null) {
            try {
                while (rs.next()) {
                    count = rs.getInt("count");
                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                closedAll(rs);
            }
        }
        return count;
    }

    @Override
    public int addEmp(Emp emp) {
        String sql = "INSERT INTO employee(emp_name,dep_id,position,start_date,phone,img_url) VALUES (?,?,?,?,?,?)";
        return executeUpdate(sql,emp.getEmpName(),emp.getDeptId(),emp.getPosition(),emp.getStartDate(),emp.getPhone(),emp.getImgUrl());
    }

    @Override
    public int deleteEmp(int id) {
        String sql = "DELETE FROM employee WHERE id = ?";
        return executeUpdate(sql,id);
    }

    @Override
    public int updateEmp(Emp emp) {
        String sql = "UPDATE employee SET emp_name = ?,dep_id = ?,position = ?,start_date = ?,phone = ?,img_url = ? WHERE id = ?";
        return executeUpdate(sql,emp.getEmpName(),emp.getDeptId(),emp.getPosition(),emp.getStartDate(),emp.getPhone(),emp.getImgUrl(),emp.getId());
    }

    @Override
    public Emp getEmpById(int id) {
        Emp emp = null;
        String sql = "SELECT * FROM employee WHERE id = ?";
        ResultSet rs = executeQuery(sql,id);
        if (rs != null) {
            try {
                while (rs.next()) {
                    emp = new Emp();
                    emp.setId(rs.getInt("id"));
                    emp.setEmpName(rs.getString("emp_name"));
                    emp.setDeptId(rs.getInt("dep_id"));
                    emp.setPosition(rs.getString("position"));
                    emp.setStartDate(rs.getString("start_date"));
                    emp.setPhone(rs.getString("phone"));
                    emp.setImgUrl(rs.getString("img_url"));

                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                closedAll(rs);
            }
        }
        return emp;
    }
}


